package com.jk.wh.controller;

import com.jk.CarouselBean;
import com.jk.wh.service.CarouselService;
import com.jk.wh.utils.UploadImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
    * @Author: wh
    * @Description: 轮播图查询
    * @Date: 2021/5/20 18:03
    * @Param:[]
    * @Return: java.util.List<com.jk.CarouselBean>
    **/
    @RequestMapping("queryCarousel")
    public List<CarouselBean> queryCarousel(){
        return carouselService.queryCarousel();
    }

    /**
    * @Author: wh
    * @Description: 图片上传OSS
    * @Date: 2021/5/20 20:00
    * @Param:[upimg, request]
    * @Return: java.lang.String
    **/
    @RequestMapping("img")
    public String upload(MultipartFile upimg, HttpServletRequest request) throws Exception{
        String saveFile = UploadImg.getUrl(upimg);
        request.getSession().setAttribute("file",saveFile);
        System.out.println(saveFile);
        String a="{\"path\":\"" + saveFile + "\"}";
        return a;
    }

    /**
    * @Author: wh
    * @Description: 新增or修改
    * @Date: 2021/5/20 20:00
    * @Param:[carousel]
    * @Return: void
    **/
    @RequestMapping("addCarousel")
    public void addCarousel(CarouselBean carousel){
        carouselService.addCarousel(carousel);
    }

    /**
    * @Author: wh
    * @Description: 删除轮播图
    * @Date: 2021/5/20 20:05
    * @Param:[id]
    * @Return: void
    **/
    @RequestMapping("del")
    public void del(int id){
        carouselService.del(id);
    }

    /**
    * @Author: wh
    * @Description: 修改回显
    * @Date: 2021/5/20 20:15
    * @Param:[id]
    * @Return: com.jk.CarouselBean
    **/
    @RequestMapping("upById")
    public CarouselBean upById(int id){
        return carouselService.upById(id);
    }

}
